package com.engineering.elink.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.PersonCustomCo;
import com.engineering.elink.common.TypeEnum;
import com.engineering.elink.dao.PersonCustomMapper;
import com.engineering.elink.dao.PersonMapper;
import com.engineering.elink.dao.UserMapper;
import com.engineering.elink.domain.Person;
import com.engineering.elink.domain.PersonCustom;
import com.engineering.elink.service.PersonCustomService;

@Service
public class PersonCustomServiceImpl implements PersonCustomService {

	@Autowired
	private PersonCustomMapper personCustomMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private PersonMapper personMapper;

	
	/**
	 * 初始化用户个人中心自定义模块
	 * @param String userId 用户ID
	 * @return List<PersonCustom> 个人中心自定义模块列表
	 */
	@Override
	public List<PersonCustom> getPersonCustomList(String userId) {
		List<PersonCustom> list = personCustomMapper.selectAll(userId);
		System.out.println("大小"+list.size());
		if(list.size() ==0){//初始化
			initPersonCustom( userId);
			list =  personCustomMapper.selectAll(userId);
		}
		return list;
	}

	
	@Override
	public Object addCustom(List<PersonCustomCo> pcList,String userId) {
		//先删掉旧的
		personCustomMapper.delByUserId(userId);	
		personCustomMapper.insertBatch(pcList);
		return null;
	}

	@Override
	public Object modify(int moduleId, String userId) {
		//首先校验该模块是否存在
		PersonCustom pc = personCustomMapper.selectByUnion(moduleId,userId);
		int maxSeq = personCustomMapper.getMaxSeq(userId);
		if(pc == null){//不存在则插入数据
			PersonCustom personCustom = new PersonCustom ();
			personCustom.setIsDisplay(0);
			personCustom.setIsDelete(0);
			personCustom.setModuleId(moduleId);
			personCustom.setSequence(maxSeq);
			personCustom.setUserId(userId);
			personCustomMapper.insertSelective(personCustom);
		}else{//更新可见状态
			personCustomMapper.update(moduleId,userId);
		}
		return null;
	}
	
	/**
	 * 初始化用户个人中心自定义模块
	 * @param String userId 用户ID
	 * @return void
	 */

	@Transactional
	public void initPersonCustom(String userId) {
		Person person = personMapper.selectByUserId(userId);
		if (person == null) {
			// TODO throw异常 用户信息读取失败
		}
		// 初始化个人中心自定义模块
		Integer[] CustomTypes = { 
				TypeEnum.PersonCustomCarrerModule.getIndex(), 
				TypeEnum.PersonCustomDatabaseModule.getIndex(),
				TypeEnum.PersonCustomVideoModule.getIndex(), 
				TypeEnum.PersonCustomLibraryModule.getIndex(), 
				TypeEnum.PersonCustomPostbarModule.getIndex(),
				TypeEnum.PersonCustomNewsModule.getIndex() };
		// 职业规划体系 工程资料库 工程e学堂 论文图书馆 百问百答 新闻资讯
		for (int i = 0; i < CustomTypes.length; i++) {
			PersonCustom personCustom = new PersonCustom();
			// 用户为学生党 则在初始化个人中心自定义主页模版中 去除论文图书馆模块
			if (person.getCarrerStatusId() == TypeEnum.CarrerStatusForStudent.getIndex() 
					&& CustomTypes[i] == TypeEnum.CarrerStatusForStudent.getIndex()) {
				personCustom.setIsDisplay(1);
				personCustom.setIsDelete(1);
				personCustom.setModuleId(CustomTypes[i]);
				personCustom.setSequence(i);
				personCustom.setUserId(userId);
			} else {
				personCustom.setIsDisplay(0);
				personCustom.setIsDelete(0);
				personCustom.setModuleId(CustomTypes[i]);
				personCustom.setSequence(i);
				personCustom.setUserId(userId);
			}
			personCustomMapper.insertSelective(personCustom);
		}
	}
}
